
/**
 * Created by Administrator on 2017-11-25
 */

define(function (require, exports, module) {
    var uuid = require('node-uuid');
    'use strict';
    var payid;
    var spotid;
    function comfirm() {
        var saleids;
        var prices=0;
        for(var i=0;i<reservearray.length;i++){
            prices+=(reservearray[i].price*reservearray[i].count);
        }
        $.ajax({
            type: "POST",
            url: "/cart/advancewait",
            data: {
                reservearray:reservearray
            },
            dataType: "json",
            success: function (res) {
                spotid=res.data.data[0].spotid;
                payid=res.data.data[0].payid;
                window.location.href='http://www.eyou96001.com/order_pay.html?payid='+payid +'&price='+prices+'&userid='+user.id;
            }
        });
        // $("#reservemenu").slideDown();
        // $(".backcolor").css("opacity", "0.7");
        // $(".backcolor").css("display", "block");
    }

    function paysucess(way) {
        var deposit=$(".earnest").html();
            $.ajax({
                url: '/cart/payearnest',
                type: 'POST',
                data: {
                    payid:payid,
                    spotid:spotid,
                    userid:user.id,
                    way:way,
                    deposit:deposit
                },
                success: function (res) {
                    if (!res.success) {
                        alert(res.msg);
                        return;
                    }else{
                         var contact=res.data.rows3;
                                seajs.use("/script/shoppingcart/contact", function () {
                                    $("#scenicbody").html(new EJS({url: "/views/shoppingcart/contact.ejs"}).render({
                                        contact: contact
                                    }));
                                });
                        $("#bar").show();
                        $("#reservemenu").hide();
                        $("#paysuss").show();
                        $(".backcolor").css("display", "block");
                    }
                }
            })

    }

    /**
     * 支付
     */
    function payMoney(type,yearDatas){
        var saleids=[];//
        var counts=[];
        var commodityids=[];
        var freights=[];
        var prices=[];
        var sources=[];
        // for(var i=0;i<SHOPCART_DATAS.length;i++){
        //     saleids.push(SHOPCART_DATAS[i].product.id);
        //     counts.push(SHOPCART_DATAS[i].count);
        //     sources.push(SHOPCART_DATAS[i].type);
        //     commodityids.push(SHOPCART_DATAS[i].product.commodity_id)
        //     freights.push(SHOPCART_DATAS[i].product.freight)
        //     prices.push(SHOPCART_DATAS[i].product.price)
        // }
        for(var i=0;i<reservearray.length;i++){
            saleids.push(reservearray[i].id);
            counts.push(reservearray[i].counts);
            prices.push(reservearray[i].price);
        }
        var params={
            userid:user.id,
            saleids: saleids,
            counts: counts,
            addressid:addressid,
            commodityids:commodityids,
            freights:freights,
            sources:sources,
            prices:prices,
            type:type,
            spotid:sid,
            prizerelationid: prizerelationid? prizerelationid: null,
            isyearcard:yearDatas?1:0
        }
        // console.log(counts)
        $.ajax({
            url: '/ticket/buyTicketRecord',
            type: 'GET',
            data: {
                uid: user.id,
                ids: saleids,
                counts: counts
            },
            success: function (res) {
                if(!res.success) return toast.show(res.msg);
                $.post("/pay/generate_exchange",params,function(result){

                    if(result.success){
                        // $.post("/pay/yearcard-application",yearDatas , function (result) {})
                        if(type==1)//微信支付
                            weixin_onBridgeReady(result.data);
                        else//支付宝
                            ali_onBridgeReady(result.data);
                    }
                    else{
                        toast.show("保存商品信息失败！")
                    }
                })
            }
        })

    }

    function weixin_onBridgeReady(pay){
        //console.log(pay)
        wx.chooseWXPay({
            timestamp: pay.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
            nonceStr:pay.nonceStr, // 支付签名随机串，不长于 32 位
            package: pay.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
            signType:pay.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
            paySign: pay.paySign , // 支付签名
            success: function (res) {
                // 支付成功后的回调函数
                //alert($.getJSON(res) );
                //alert (JSON.stringify(res))
                if(res.errMsg == "chooseWXPay:ok" ) {
                    // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                    toast.show('支付成功！');


                }else{
                    alert(res.errMsg);
                }
            },
            cancel:function(res){
                //支付取消
                //跳转到商城
                toast.show('支付取消！');

            }
        });
    }

    /**
     * 阿里支付
     */
    function ali_onBridgeReady(info){
        //预生成页面信息，跳转到
        window.location.href=SPOTURL.alipay+"?WIDout_trade_no="+info.out_trade_no+"&WIDtotal_amount="+info.money+"&sid="+sid+"&payid="+info.id+"&pagename=share_zfb";
    }
    return {
        comfirm:comfirm,
        paysucess:paysucess,
        payMoney:payMoney
    }
});